<template>
  <div class="resource-category-list">
    <el-card class="box-card">
      <el-button @click="handleAdd">添加</el-button>
      <el-divider></el-divider>
      <el-table
        :data="categories"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
      >
        <el-table-column
          type="index"
          label="编号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          width="180">
        </el-table-column>
        <el-table-column
          width="180"
          prop="createdTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          type="sort"
          label="排序"
          width="100">
        </el-table-column>
        <el-table-column
          width="180"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :title="isEdit ? '编辑资源' : '添加资源'"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <create-or-edit
        v-if="dialogVisible"
        :category-record="categoryRecord"
        :is-edit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import CreateOrEdit from './components/CategoryCreateOrEdit.vue'
import { getResourceCategories, deleteCategory } from '@/services/resource-category'

export default Vue.extend({
  name: 'CategoriesList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      categories: [], // 资源列表
      isLoading: true, // 加载状态,
      categoryRecord: null,
      dialogVisible: false,
      isEdit: false
    }
  },

  created () {
    this.loadCategories()
  },

  methods: {
    async loadCategories () {
      this.isLoading = true // 展示加载中状态
      const { data } = await getResourceCategories()
      this.categories = data.data
      this.isLoading = false // 关闭加载中状态
    },

    handleEdit (category: any) {
      this.dialogVisible = true
      this.categoryRecord = category
      this.isEdit = true
    },

    async handleDelete (item: any) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async () => { // 确认执行这里
          // 请求删除操作
          const { data } = await deleteCategory(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadCategories() // 更新数据列表
          }
        })
        .catch(err => { // 取消执行这里
          console.log(err)
          this.$message.info('已取消删除')
        })
    },

    onSuccess () {
      this.dialogVisible = false // 关闭对话框
      this.loadCategories() // 重新加载数据列表
      this.categoryRecord = null
    },

    handleAdd () {
      this.isEdit = false
      this.dialogVisible = true
    }

  }
})
</script>

<style lang="scss" scoped></style>
